package cn.dw.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.dw.domain.DutyInfo;
import cn.dw.domain.People;
import cn.dw.service.DutyInfoService;

public class DutyInfoAction
  extends ActionSupport
  implements ModelDriven<DutyInfo>
{
  private static final long serialVersionUID = 3802621928629709434L;
  private DutyInfo dutyInfo = new DutyInfo();
  private DutyInfoService dis;
  private ChangetoJson ctj = new ChangetoJson();
  
  public String list()
    throws Exception
  {
    HttpServletRequest request = ServletActionContext.getRequest();
    
    request.setCharacterEncoding("utf-8");
    
    SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
    HashMap<String, Object> map = null;
    
    List<DutyInfo> dutyInfos = this.dis.getAll();
    
    DutyInfo dif = new DutyInfo();
    People peo = new People();
    List<Map<String, Object>> list = new ArrayList<>();
    if (dutyInfos.size() > 0) {
      for (int i = 0; i < dutyInfos.size(); i++)
      {
        dif = (DutyInfo)dutyInfos.get(i);
        peo = dif.getPeople();
        
        map = new HashMap<>();
        if ((request.getParameter("stime").length() != 0) && (request.getParameter("etime").length() != 0))
        {
          Date stime = day.parse(request.getParameter("stime"));
          
          Date etime = day.parse(request.getParameter("etime"));
          if ((dif.getTimes().after(stime)) && (dif.getTimes().before(etime)))
          {
            map.put("numid", dif.getNumid());
            map.put("times", dif.getTimes().toString());
            if (dif.getTimee() == null) {
              map.put("timee", null);
            } else {
              map.put("timee", dif.getTimee().toString());
            }
            map.put("name", peo.getPoint().getName());
            map.put("people", peo.getName());
            map.put("phonenum", peo.getPhone());
            
            list.add(map);
          }
        }
        else
        {
          map.put("numid", dif.getNumid());
          map.put("times", dif.getTimes().toString());
          if (dif.getTimee() == null) {
            map.put("timee", null);
          } else {
            map.put("timee", dif.getTimee().toString());
          }
          map.put("name", peo.getPoint().getName());
          map.put("people", peo.getName());
          map.put("phonenum", peo.getPhone());
          
          list.add(map);
        }
      }
    }
    this.ctj.json(list);
    return null;
  }
  
  public void setDis(DutyInfoService dis)
  {
    this.dis = dis;
  }
  
  public DutyInfo getModel()
  {
    return this.dutyInfo;
  }
}
